package com.agentai.framework.satoken.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import com.agentai.framework.satoken.enums.DeviceType;
import com.agentai.framework.satoken.helper.StpMpUtil;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;


/**
 * @Author fuzhilin
 * @Date 2023/2/18 0018 15:39
 * @Description
 */
@RequiredArgsConstructor
@Slf4j
//@AutoConfiguration
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    private final SatokenSecurityProperties securityProperties;

    // 注册 Sa-Token 拦截器，打开注解式鉴权功能
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //TODO 后续增加相应的有关url的拦截
        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
        log.info("SaTokenConfigure addInterceptors");
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
//                .excludePathPatterns(securityProperties.getExcludes())//暂时不用配置文件，防止被加入
                .excludePathPatterns("/sys/auth/captcha/**", "/sys/auth/login","/sys/auth/send/code",
                        "/sys/auth/mobile","/sys/auth/third","/sys/auth/mobile","/sys/third/callback/**"
                        ,"/sys/third/render/**"
                         //swagger相关
                        ,"/doc.html","/v3/api-docs/**","/swagger-resources/**","/webjars/**","/*/api-docs");
    }


    /**
     * Sa-Token 整合 jwt (Style模式)
     *  这种模式的话token长度比较长,配置文件配置的style就不管用了
     * @return
     */
    @Bean
    public StpLogic getStpLogicJwt() {
        return new StpLogicJwtForSimple();
    }

    /**
     * 在多账户模式中集成 jwt
     */
    @PostConstruct
    public void setStpMpLogicJwt() {
        StpMpUtil.setStpLogic(new StpLogicJwtForSimple(DeviceType.PC.getDevice()));
    }

    /**
     * 自定义dao层存储
     */
//    @Bean
//    public SaTokenDao saTokenDao() {
//        return new PlusSaTokenDao();
//    }


}
